﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Obilogic.YammerReader2010.YammerReader
{
    [ToolboxItemAttribute(false)]
    public class YammerReader : WebPart
    {
        [WebBrowsable(true)]
        [WebDisplayName("Yammer Network Name")]
        [WebDescription("Name of the Yammer Network")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Yammer Configuration")]
        public string YammerNetworkName { get; set; }

        [WebBrowsable(true)]
        [WebDisplayName("Yammer Feed Type (optional) - 'group', 'topic' or 'user'")]
        [WebDescription("can be 'group', 'topic', or 'user'")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Yammer Configuration")]
        public string YammerFeedType { get; set; }

        [WebBrowsable(true)]
        [WebDisplayName("Yammer Feed ID (optional)")]
        [WebDescription("To retrieve the numeric feed ID, please navigate to the feed on yammer and note it from the URL")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Yammer Configuration")]
        public string YammerFeedID { get; set; }


      

        protected override void Render(HtmlTextWriter writer)
        {
            this.RenderContents(writer);
            
            if (String.IsNullOrEmpty(this.YammerNetworkName) == false)
            {
                writer.Write("<script>");
                writer.Write("yam.connect.embedFeed(");
                writer.Write("{ container: '#embedded-feed'");
                writer.Write(", network: '" + this.YammerNetworkName + "' ");
                if ((String.IsNullOrEmpty(this.YammerFeedType) == false) && (String.IsNullOrEmpty(this.YammerFeedID) == false))
                {
                    writer.Write(", feedType: '" + this.YammerFeedType + "' ");
                    writer.Write(", feedId: '" + this.YammerFeedID + "' ");
                }
                writer.Write("});");
                writer.Write("</script>");
                writer.Write("<div id='embedded-feed'></div>" + "</div>");
            }
            else
            {
                writer.Write("<div>Please set Yammer Configuration in Web Part Properties<div>");
            }
            
        }
    }
}


